package tankwar4;

import java.util.Vector;

/**
 * @Auther: Tx
 * @Date: 2022/5/28 22:28
 */
//敌人的坦克
public class EnemyTank extends Tank implements Runnable {
    Vector<Bullet> bullets = new Vector<>();
    boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
//        自由移动
        while (true) {
//            当敌人的子弹小于1时创建一颗子弹
            if (bullets.size() < 1 && isLive) {
                Bullet bullet = null;
                switch (getDirection()) {
                    case 0://向上开火
                        bullet = new Bullet(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        bullet = new Bullet(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        bullet = new Bullet(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        bullet = new Bullet(getX(), getY() + 20, 3);
                        break;
                }
                bullets.add(bullet);
                new Thread(bullet).start();
            }

            switch (getDirection()) {
                case 0:
                    for (int i = 0; i < 30; i++) {//保持一个方向走三十步
                        if (getY() > 0) {
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {//保持一个方向走三十步
                        if (getX() + 60 < 1000)
                            moveRight();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {//保持一个方向走三十步
                        if (getY() + 60 < 750)
                            moveDown();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {//保持一个方向走三十步
                        if (getX() > 0)
                            moveLeft();
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            this.setDirection(((int) (Math.random() * 4)));
            if (!isLive) {
                break;
            }
        }
//        线程结束

    }

}
